package com.common.frame.utils;

import androidx.lifecycle.LifecycleOwner;

import com.jakewharton.rxrelay2.PublishRelay;
import com.jakewharton.rxrelay2.Relay;

import io.reactivex.Observable;

/**
 * RxBusUtils
 * create by sunxy on 2020/9/10
 **/
public class RxBusUtils {

    private static volatile RxBusUtils sRxBus;
    private final Relay<Object> mBus;

    public static RxBusUtils get() {
        if (sRxBus == null) {
            synchronized (RxBusUtils.class) {
                if (sRxBus == null) {
                    sRxBus = new RxBusUtils();
                }
            }
        }
        return sRxBus;
    }

    private RxBusUtils() {
        this.mBus = PublishRelay.create().toSerialized();
    }

    public <T> Observable<T> Observable(Class<T> clazz){
        return mBus.ofType(clazz);
    }

    public <T> Observable<T> Observable(LifecycleOwner owner, Class<T> clazz){
        return Observable(clazz);
    }

    public <T> void post(T t){
        mBus.accept(t);
    }

}
